//
//  FansViewController.m
//  Cat
//
//  Created by lushitong on 2018/12/9.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "FansViewController.h"
#import "FansTableView.h"
#import "HttpUserManager.h"
#import "InviteViewController.h"

@interface FansViewController () <QKSegmentControlDelegate,UIScrollViewDelegate>

@property (nonatomic, strong) UIImageView *topView;
@property (nonatomic, strong) UILabel *fansLabel;
@property (nonatomic, strong) UILabel *fanNumberLabel;
@property (nonatomic, strong) UILabel *incomeLabel;
@property (nonatomic, strong) UIButton *bottomButton;
@property (nonatomic, strong) UIScrollView *mainScrollView;
@property (nonatomic, strong) FansTableView *allTableView;
@property (nonatomic, strong) FansTableView *inviteTableView;
@property (nonatomic, strong) FansTableView *recomTableView;

@property (nonatomic, strong) QKSegmentControl *bottomSegMentView; // 底部滑动的视图

@end

@implementation FansViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	[self loadAllSubviews];
	self.view.backgroundColor = [UIColor whiteColor];
	[self.topTitleView setDetailDic:@{Nav_Title:@"我的粉丝"}.mutableCopy];
	[self.view bringSubviewToFront:self.topTitleView];
	
	[self request];
}

- (void)request {
	[HttpUserManager getFansCountInfo:^(NSNumber * _Nonnull total, NSString * _Nonnull amount) {
		self.fanNumberLabel.text = [NSString stringWithFormat:@"%@",total.stringValue];
		self.incomeLabel.text = [NSString stringWithFormat:@"贡献收益 ￥%@",amount];
	} failure:^(NSInteger code, NSString * _Nonnull message) {
		[FLProgressHUD showError:message];
	}];
}

- (void)loadAllSubviews {
	[self.view addSubview:self.topView];
	[self.view addSubview:self.mainScrollView];
	[self.mainScrollView addSubview:self.allTableView];
	[self.view addSubview:self.bottomSegMentView];
	[self.view addSubview:self.bottomButton];
	
	[self.topView addSubviews:@[self.fansLabel,self.fanNumberLabel,self.incomeLabel]];
	
	[self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(self.topTitleView.mas_bottom).offset(kQKFitModuleDecimal(16.0));
		make.left.equalTo(self.view.left).offset(kQKFitModuleDecimal(16.0));
		make.right.equalTo(self.view.right).offset(-kQKFitModuleDecimal(16.0));
		make.height.equalTo(@(kQKFitModuleDecimal(118.0)));
	}];
	
	[self.fansLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(self.topView.mas_top).offset(kQKFitModuleDecimal(21.0));
		make.centerX.equalTo(self.topView.mas_centerX);
	}];
	
	[self.fanNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.centerX.equalTo(self.fansLabel.mas_centerX);
		make.top.equalTo(self.fansLabel.mas_bottom).offset(kQKFitModuleDecimal(9.0));
	}];
	
	[self.incomeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.centerX.equalTo(self.fanNumberLabel.mas_centerX);
		make.top.equalTo(self.fanNumberLabel.mas_bottom).offset(kQKFitModuleDecimal(7));
	}];
	
	[self.bottomButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.and.right.and.bottom.equalTo(self.view);
		make.height.equalTo(@(kQKFitModuleDecimal(50)));
	}];
	
	[self.bottomSegMentView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.topView.mas_left);
		make.top.equalTo(self.topView.mas_bottom);
		make.size.mas_equalTo(CGSizeMake(kQKFitModule(128), kQKFitModule(50)));
	}];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
	CGFloat contentOffsetX = scrollView.contentOffset.x;
	[self.bottomSegMentView changeBottomLineViewPointX:contentOffsetX anitmated:NO];
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
	CGFloat contentOffsetX = scrollView.contentOffset.x;
	NSInteger currentIndex = (NSInteger)(contentOffsetX / ScreenWidth);
	[self.bottomSegMentView changeSelectedSegmentIndex:currentIndex anitmated:NO];
	if (currentIndex == 1 && ![self.mainScrollView.subviews containsObject:self.inviteTableView]) {
		[self.mainScrollView addSubview:self.inviteTableView];
	} else if (currentIndex == 2 && ![self.mainScrollView.subviews containsObject:self.recomTableView]) {
		[self.mainScrollView addSubview:self.recomTableView];
	}
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
	[self scrollViewDidEndDecelerating:scrollView];
}

- (void)clickButton:(UIButton *)button {
	 [self.mainScrollView setContentOffset:CGPointMake(ScreenWidth * button.tag, 0) animated:YES];
}

- (void)clickButton:(UIButton *)button segement:(QKSegmentControl *)segement {
	
}

#pragma mark----- 点击方法 -----

- (void)getInvite {
	InviteViewController *invite = [[InviteViewController alloc] init];
	[self.navigationController pushViewController:invite animated:YES];
}

#pragma mark----- lazy init -----

- (UIImageView *)topView {
	if (!_topView) {
		_topView = [[UIImageView alloc] init];
		_topView.image = [UIImage createImageWithSize:CGSizeMake(ScreenWidth - 2 * kQKFitModuleDecimal(16.0), kQKFitModuleDecimal(118)) gradientColors:@[COLOR_WITH_HEX(0xFFD574),COLOR_WITH_HEX(0xFDB430)] percentage:@[@(0),@(1)] gradientType:GradientFromTopToBottom];
		_topView.layer.cornerRadius = kQKFitModuleDecimal(6.0);
		_topView.layer.masksToBounds = YES;
	}
	return _topView;
}

- (UILabel *)fansLabel {
	if (!_fansLabel) {
		_fansLabel = [UILabel labelWithText:@"我的粉丝" font:[UIFont systemFontOfSize:kQKFitFont(10)] textColor:COLOR_WITH_HEX(0x57524F)];
	}
	return _fansLabel;
}

- (UILabel *)fanNumberLabel {
	if (!_fanNumberLabel) {
		_fanNumberLabel = [UILabel labelWithText:@"300" font:[UIFont boldSystemFontOfSize:kQKFitFont(24)] textColor:COLOR_WITH_HEX(0x57524F)];
	}
	return _fanNumberLabel;
}

- (UILabel *)incomeLabel {
	if (!_incomeLabel) {
		_incomeLabel = [UILabel labelWithText:@"贡献收益 ￥100.00" font:[UIFont systemFontOfSize:kQKFitFont(12)] textColor:COLOR_WITH_HEX(0xFFFFFF)];
	}
	return _incomeLabel;
}

- (UIButton *)bottomButton {
	if (!_bottomButton) {
		_bottomButton = [UIButton buttonWithTitle:@"立即邀请" color:COLOR_WITH_HEX(0x57524F) font:[UIFont boldSystemFontOfSize:kQKFitFont(16)]];
		[_bottomButton setBackgroundImage:[UIImage imageWithColor:COLOR_WITH_HEX(0xFFC236)] forState:UIControlStateNormal];
		[_bottomButton addTarget:self action:@selector(getInvite) forControlEvents:UIControlEventTouchUpInside];
	}
	return _bottomButton;
}

- (QKSegmentControl *)bottomSegMentView {
	if (!_bottomSegMentView) {
		_bottomSegMentView = [[QKSegmentControl alloc] initWithFrame:CGRectZero withTitles:@[@"全部",@"邀请"] style:QKSegmentControlStytleBoldLine];
		_bottomSegMentView.selectedFontSize = kQKFitFont(20);
		_bottomSegMentView.fontSize = kQKFitFont(14);
		_bottomSegMentView.selectedColor = COLOR_WITH_HEX(0x000000);
		_bottomSegMentView.normalColor = COLOR_WITH_HEX(0x999999);
		_bottomSegMentView.bottomLineColor = COLOR_WITH_HEX(0xFFD600);
		_bottomSegMentView.bottomLineHeight = kQKFitModuleDecimal(4);
		_bottomSegMentView.bottomLinePaddingHeight = kQKFitModuleDecimal(4);
		_bottomSegMentView.bottomLineWidth = kQKFitModule(30);
		_bottomSegMentView.needGradient = YES;
		_bottomSegMentView.gradientColors = @[COLOR_WITH_HEX(0xFFD600),COLOR_WITH_HEX(0xFFA9A9)];
		_bottomSegMentView.delegate = self;
	}
	return _bottomSegMentView;
}


- (UIScrollView *)mainScrollView {
	if (!_mainScrollView) {
		_mainScrollView =  [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, self.topTitleView.bottom + kQKFitModuleDecimal(134 + 50), ScreenWidth, ScreenHeight - kQKFitModuleDecimal(134 + 64 + 50))];
		_mainScrollView.delegate = self;
		_mainScrollView.pagingEnabled = YES;
		_mainScrollView.showsVerticalScrollIndicator = NO;
		_mainScrollView.showsHorizontalScrollIndicator = NO;
		_mainScrollView.bounces = NO;
		[_mainScrollView setContentSize:CGSizeMake(self.bottomSegMentView.titles.count * ScreenWidth , 0)];
		_mainScrollView.backgroundColor = [UIColor whiteColor];
	}
	return _mainScrollView;
}

- (FansTableView *)allTableView {
	if (!_allTableView) {
		_allTableView = [[FansTableView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.mainScrollView.width, self.mainScrollView.height) sections:1 andType:@""];
	}
	return _allTableView;
}

- (FansTableView *)inviteTableView {
	if (!_inviteTableView) {
		_inviteTableView = [[FansTableView alloc] initWithFrame:CGRectMake(ScreenWidth, 0.0, self.mainScrollView.width, self.mainScrollView.height) sections:1 andType:@"1"];
	}
	return _inviteTableView;
}

@end
